<?php
/**
 * Miffos framework
 * ----------------------------------------------
 *
 * Copyright (c) 2008, 2009 Martin Sadový (http://sodae.cz)
 *
 * This source file is subject to the "Miffos license" that is bundled
 * with this package in the file license.txt.
 *
 * @copyright  Copyright (c) 2008, 2009 Martin Sadový
 * @license    license.txt
 * @link       http://miffos.sodae.cz
 * @package    miffos
 * @version    $Id$
 */

  class FormSelect extends FormControl
  {
    protected $tag = 'select';

    public function __construct($form, $name, $label, $data, $selected=null)
    {
        parent::__construct($form, $name, $label);
        $this->decodeData($this->element,$data,$selected);
    }

    private function decodeData(&$instance,$data,$selected)
    {
        foreach($data as $key=>$value){
            if(is_array($value)){
               $inst = $instance->create('optgroup')->label($key);
               $this->decodeData($inst,$value,$selected);
            }elseif(is_string($value)){
                $option = $instance->create('option')->value($key)->setText($value);
                if(!is_null($selected) AND $selected === $key)
                    $option->selected(true);
            }
        }
    }
  }
?>